using System.ComponentModel.DataAnnotations;
using FytSoa.Common.Enum;
using FytSoa.Domain.Crm;
using FytSoa.Domain.Hr;
using SqlSugar;

namespace FytSoa.Domain.Erp;

/// <summary>
/// 生产订单
/// </summary>
[SugarTable("erp_produce_order")]
public class ErpProduceOrder:Entity
{
    /// <summary>
    /// 生产编号
    /// </summary>
    [Required]
    [StringLength(30)]
    public string Number { get; set; }

    /// <summary>
    /// 订单名称
    /// </summary>
    [StringLength(100)]
    public string Name { get; set; }

    /// <summary>
    /// 项目订单编号
    /// </summary>
    [Required]
    public long ProjectId { get; set; }
    
    /// <summary>
    /// 项目订单
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(ProjectId))]
    public CrmProject Project { get; set; }

    /// <summary>
    /// 生产负责人
    /// </summary>
    [Required]
    public long ChargePersonId { get; set; }
    
    /// <summary>
    /// 负责人
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(ChargePersonId))]
    public HrEmployee Employee { get; set; }

    /// <summary>
    /// 数量
    /// </summary>
    [Required]
    public int Count { get; set; } = 0;

    /// <summary>
    /// 接单日期
    /// </summary>
    public DateTime? AcceptTime { get; set; }
    
    /// <summary>
    /// 是否申请物料
    /// </summary>
    public bool IsApply { get; set; } = false;

    /// <summary>
    /// 计划完成时间
    /// </summary>
    public DateTime? DesignTime { get; set; }
    
    /// <summary>
    /// 是否需要设计
    /// </summary>
    public bool IsDesign { get; set; } = false;
    
    /// <summary>
    /// 设计过程
    /// </summary>
    [SugarColumn(IsJson = true)]
    public DesignProcessModel DesignProcess { get; set; } = new();
    
    /// <summary>
    /// 设计负责人
    /// </summary>
    [Required]
    public long DesignPersonId { get; set; }
    
    /// <summary>
    /// 设计负责人
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(DesignPersonId))]
    public HrEmployee DesignEmployee { get; set; }

    /// <summary>
    /// 设计完成时间
    /// </summary>
    public DateTime? ProduceTime { get; set; }
    
    /// <summary>
    /// 阶段 1/下单2/工程3/设计4/制作5/品控6/完成
    /// </summary>
    public ErpProducePhaseEnum Phase { get; set; } = ErpProducePhaseEnum.Placed;

    /// <summary>
    /// 审核 0=未审核
    /// </summary>
    public ErpAuditEnum Status { get; set; } = ErpAuditEnum.NotReviewed;

    /// <summary>
    /// 附件
    /// </summary>
    [SugarColumn(IsJson = true)]
    public List<FileBase> Files { get; set; } = new();
    
    /// <summary>
    /// 品控状态0待接收/1已接收/2已开工/3已完成
    /// </summary>
    public int ControlStatus { get; set; } = 0;
    
    /// <summary>
    /// 品控工时
    /// </summary>
    public double ControlHour { get; set; } = 0;

    /// <summary>
    /// 品控进度
    /// </summary>
    [SugarColumn(IsJson = true)]
    public List<DesignProcessModel.ProcessModel> ControlProcess { get; set; } = new();

    /// <summary>
    /// 品控驳回原因及回复
    /// </summary>
    [SugarColumn(IsJson = true)]
    public ControlCauseModel ControlCause { get; set; } = new();

    /// <summary>
    /// 备注说明
    /// </summary>
    public string Remark { get; set; }

    /// <summary>
    /// 自定义属性
    /// </summary>
    [SugarColumn(IsJson = true)]
    public List<KeyValueBase> CustomAttr { get; set; } = new();
    
    /// <summary>
    /// 关闭原因
    /// </summary>
    public string CloseRemark { get; set; }

    /// <summary>
    /// 创建人
    /// </summary>
    public string CreateUser { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    [Required]
    public DateTime CreateTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 更新人
    /// </summary>
    public string UpdateUser { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdateTime { get; set; }

    /// <summary>
    /// 品控原因及回复
    /// </summary>
    public class ControlCauseModel
    {
        /// <summary>
        /// 原因
        /// </summary>
        public string Cause { get; set; }
        
        /// <summary>
        /// 回复
        /// </summary>
        public string Reply { get; set; }
    }


    public class DesignProcessModel
    {
        /// <summary>
        /// 工时
        /// </summary>
        public double Hour { get; set; }

        public List<ProcessModel> Process { get; set; } = new();

        public class ProcessModel
        {
            /// <summary>
            /// 时间
            /// </summary>
            public DateTime Time { get; set; } = DateTime.Now;

            /// <summary>
            /// 状态 1=接受 2=已开工  3=已完成
            /// </summary>
            public int Status { get; set; }
            
            /// <summary>
            /// 操作人姓名
            /// </summary>
            public string EmployeeName { get; set; }

            /// <summary>
            /// 操作人姓名
            /// </summary>
            public long EmployeeId { get; set; } = 0;
        }
    }
}